package com.ucar.umock.client.config.memory;

import com.ucar.umock.client.config.UMockMethodConfigOperate;
import com.ucar.umock.client.config.UMockMethodConfigBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName UMockMethodConfigMemoryOperate
 * @Description TODO
 * @Author zhanbs baoshan.zhan@ucarinc.com
 * @Date 2020/4/2 15:24
 * @Version 1.0
 **/
public class UMockMethodConfigMemoryOperate implements UMockMethodConfigOperate {

    private Map<String,UMockMethodConfigBean> container=new ConcurrentHashMap<String,UMockMethodConfigBean>();

    public UMockMethodConfigBean getMockMethodConfig(String key) {
        Assert.notNull(key,"获取Mock 配置数据Key 不为空");
        UMockMethodConfigBean data=container.get(key);
        Assert.notNull(data,"获取Mock key="+key+"不存在数据");
        return data;
    }

    public void putMockMethodConfig(UMockMethodConfigBean mockMeta) {
        Assert.notNull(mockMeta,"mock 对象不为空");
        String key=mockMeta.getKey();
        Assert.notNull(key,"mock 配置key 不为空");
        Assert.isTrue(!container.containsKey(key),"mock 配置数据key="+mockMeta.getKey()+"，存在重复");
        container.put(key,mockMeta);
    }

    public void updateMockSwitch(String key, boolean mockSwitch) {
        UMockMethodConfigBean mockMeta=this.getMockMethodConfig(key);
        mockMeta.setMockSwitch(mockSwitch);
    }
    public void updateMockSwitchByClazz(String className,boolean mockSwitch){
        for(UMockMethodConfigBean item:container.values()){
            String cName=item.getClassName();
            if(StringUtils.equals(cName,className)){
                item.setMockSwitch(mockSwitch);
            }
        }
    }

    public Collection<UMockMethodConfigBean> getAllUMockMethodConfig() {
        return container.values();
    }
}
